package com.sitc.administrator.application.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sitc.administrator.application.R;
import com.sitc.administrator.application.activity.EmptyActivity;
import com.sitc.administrator.application.adapter.StationInfosAdapter;
import com.sitc.administrator.application.domain.StationInfos;
import com.sitc.administrator.application.domain.StationInfos.RowsBean;
import com.sitc.administrator.application.network.Api;
import com.sitc.administrator.application.network.reactivex.HttpListener;
import com.sitc.administrator.application.util.StaticValue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class MainHomeFragment extends BaseCommonFragment {

    private Integer id;

    private TextView station_num_tv;
    private ListView mlv;
    private StationInfosAdapter adapter;



    public static MainHomeFragment newInstance(int id) {

        Bundle args = new Bundle();
        args.putSerializable(StaticValue.ID, id);
        MainHomeFragment fragment = new MainHomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initViews() {
        super.initViews();

        station_num_tv = this.findViewById(R.id.stations_num);
        mlv = (ListView) findViewById(R.id.lv);


    }

    @Override
    protected void initDatas() {
        super.initDatas();
        getstationinfo();


    }

    @Override
    protected void initListener() {
        super.initListener();


        this.findViewById(R.id.home_stations_list).setOnClickListener(this);
        this.findViewById(R.id.home_stations_pos).setOnClickListener(this);
        this.findViewById(R.id.home_stations_energy).setOnClickListener(this);



    }


    @Override
    protected View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_home,null);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.home_stations_list:
                startActivity(EmptyActivity.class);
                getstationinfo();
                break;

            case R.id.home_stations_pos:
                startWebView(getResources().getString(R.string.home_stations_pos)
                        ,"file:///android_asset/apps/webapp/web/stations_map.html",null);
                break;
            case R.id.home_stations_energy:
//                startWebView(getResources().getString(R.string.home_stations_energy)
//                        ,"file:///android_asset/webapp/web/echarts.html");
                startWebView(getResources().getString(R.string.home_stations_energy)
                        ,"file:///android_asset/apps/webapp/web/echarts.html",null);
                break;
            case R.id.home_stations_toltal_energy:
                startWebView(getResources().getString(R.string.home_stations_toltal_energy)
                        ,"file:///android_asset/apps/webapp/web/svg.html",null);
                break;

            default:
                //如果当前界面没有处理，就调用父类的方法
                super.onClick(v);
                break;
        }





    }



    public void getstationinfo(){

        Map<String, String> params = new HashMap<String, String>();

        params.put("sysId", "SITCAPP");
        params.put("token", sp.getToken());

        Api.getInstance().getStationForSelect(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpListener<StationInfos>(getMainActivity()) {
                    @Override
                    public void onSucceeded(StationInfos data) {
                        super.onSucceeded(data);

                        int total = data.getTotal();
                        station_num_tv.setText(String.valueOf(total));
                        final List<RowsBean> rows = data.getRows();
                        adapter = new StationInfosAdapter(getContext(),rows);
                        mlv.setAdapter(adapter);





                    }
                });


    }




}
